package view.hall
{
	import control.hall.PlayerListControl;
	import control.panel.IPanelControl;
	
	import core.Control;
	
	import events.TabEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import model.hall.ListData;
	
	import utils.DisplayTool;
	
	import view.components.SimpleLoading;
	import view.components.UIComponents;
	import view.components.tab.Tab;
	import view.components.tab.TabBar;
	import view.panel.AbsPanel;
	
	public class PlayerList extends AbsPanel
	{
		private var tab:Tab;
		private var scrollPane:*;
		private var items:Array;
		private var itemLayer:Sprite;
		private var currentSelectedItem:ListItem;
		private var simpleLoading:SimpleLoading;
		private var _control:Control = Control.getInstance();
		
		
		public function PlayerList(panelControl:IPanelControl)
		{
			super(panelControl);
			resetBg(170,275);
			initTab();
			initTileList();	
			initLoading();
		}
		
		private function initLoading():void
		{
			simpleLoading = new SimpleLoading();
		}
		
		public function showLoading():void
		{
			simpleLoading.x = (this.width - simpleLoading.width) * .5 + simpleLoading.width * .5;
			simpleLoading.y = (this.height - simpleLoading.height) * .5 + simpleLoading.height* .5;
			simpleLoading.start();
			addChild(simpleLoading);
		}
		
		public function hideLoading():void
		{
			simpleLoading.stop();
			DisplayTool.removeChild(this,simpleLoading);
		}
		
		private function initTab():void
		{
			tab = new Tab();
			tab.addTabBar(new TabBar(0,"房间",56),true);
			tab.addTabBar(new TabBar(0,"好友",56));
			tab.addTabBar(new TabBar(0,"牌友",56));
			tab.y = - 29;
			
			addChild(tab);
			tab.addEventListener(TabEvent.TABBAR_CLICK,onTabBarClick);
		}
		
		public function switchToTab(index:int):void
		{
			tab.selectedIndex = index;
		}
		
		private function onTabBarClick(event:TabEvent):void
		{
			(panelControl as PlayerListControl).onTabSwitch(tab.selectedIndex);
		}
		
		private function initTileList():void
		{
			itemLayer = new Sprite();
			scrollPane = UIComponents.getScrollPane(164,269);
			scrollPane.y = 3;
			scrollPane.x = 3;
			addChild(scrollPane);
			
			items = [];
			//test();
			
		}
		
		private function test():void
		{
			var datas:Array = [];
			var listData:ListData;
			for (var i:int=0; i<10; i++)
			{
				listData = new ListData();
				listData.userName = "玩家" + i;
				listData.money = "1000" + i;
				listData.state = "正在游戏!";
				datas.push(listData);
			}
			update(datas);
		}
		
		public function update(listDatas:Array):void
		{
			clear();
			var listData:ListData;
			for (var i:int=0; i<listDatas.length; i++)
			{
				listData = listDatas[i];
				var item:ListItem = new ListItem();
				item.update(listData);
				item.y = 5 + i * (item.height + 4);
				item.x = 4;
				items.push(item);
				itemLayer.addChild(item);
				
				if (item.data.state != "2")
				{
					setItemListener(item);
				}
			}
			
			scrollPane.source = itemLayer;
		}
		
		public function clear():void
		{
			for each (var item:ListItem in items)
			{
				item.destroy();
				removeItemListener(item);
			}
			items.length = 0;
			DisplayTool.removeAllChild(itemLayer);
		}
		
		private function setItemListener(item:ListItem):void
		{
			item.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			item.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			item.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function removeItemListener(item:ListItem):void
		{
			item.removeEventListener(MouseEvent.MOUSE_OVER,onOver);
			item.removeEventListener(MouseEvent.MOUSE_OUT,onOut);
			item.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function onOver(event:MouseEvent):void
		{
			event.target.drawOverBg();
		}
		
		private function onOut(event:MouseEvent):void
		{
			event.target.drawOutBg();
		}
		
		private function onDown(event:MouseEvent):void
		{
			if (currentSelectedItem)
			{
				currentSelectedItem.selected = false;
				currentSelectedItem.drawOutBg();
			}
			
			event.target.drawSelectBg();
			event.target.selected = true;
			currentSelectedItem = event.target as ListItem;
			
			_control.playerItemPanelControl.open(currentSelectedItem);
		}
	}
}